package com.liusu.spider.util;

import com.liusu.spider.js.Array_;
import com.liusu.spider.wuzhou.Print;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;

import java.util.*;

public class Learn extends Print {

    /**
     * 函数o的实现
     * @param e
     * @param t
     * @return
     */
    public Map<String,String> x1(Map<String,String> e,String[] t){
        Map<String,String> n = new HashMap<>();
        for (String r : e.keySet()) {
            Boolean contain = Array_.contain(t, r);
            if(!contain){
                n.put(r,e.get(r));
            }
        }
        return n;
    }


    public String[] a(String[] e,String[] t){

        int r = t.length;
        int o = e.length;
        String[] x1 = new String[16];

        if(o > 0){
            for (int i = 0; i < o; i++) {
                x1[i] = e[i];
            }
        }

        for (int n = 0; n < r; n++) {
            x1[o] = t[n];
            o++;
        }
        String[] strings = Array_.copyWithoutNull(x1);
        return strings;
    }


    public String[] i(String[] e){
        return e;
    }
    public String getDrmToken(Map<String,String> e,Map<String,String> cookies){
        String t = e.get("termId");
        String n = t;
        String u = e.get("cid");
        String c = u;

        String[] ks = new String[]{"termId", "cid"};
        Map<String,String> s = this.x1(e,ks);

        String l = cookies.get("uin");
        l = l==null ? "":l;

        String d = cookies.get("skey");
        d = d==null ? "":d;

        String f = cookies.get("p_skey");
        f = f==null ? "":f;

        String p = cookies.get("p_lskey");
        p = p==null ? "":p;

        String h = "";
        String y = cookies.get("uid_type");
        y = y==null ? "":y;
        String v = cookies.get("uid_origin_uid_type");
        v = v==null ? "":v;
        String m = cookies.get("uid_origin_auth_type");
        m = m==null ? "":m;
        String g = "";

        String[] _ = new String[]{"uin=" + l, "skey=" + d, "pskey=" + f, "plskey=" + p, "ext=" + h,"uid_type=" + y,"uid_origin_uid_type=" + v,"uid_origin_auth_type=" + m,"cid=" + c,"term_id=" + n};
        Set<String> keys = s.keySet();
        String[] b = new String[keys.size()];
        int index = 0;
        for (String k : keys) {
            b[index] = k+"="+s.get(k);
            index++;
        }

        String[] P = a(a(new String[0], i(_)), i(b));
        String token = Base64.encodeBase64String(String.join(";",P).getBytes());
        return token;
    }

    /**
     * value: function (e) {
             var t, n = this.player.options, r = e.split("/");
            return r[r.length - 1] = "voddrm.token.".concat((  void 0 === t ? void 0 : t.HLSToken) || "", ".").concat(r[r.length - 1]), r[0] = "https:", e = r.join("/")
         }
     * @param e
     * @param cookies
     * @return
     */
    public String mergeUrlWithToken(String e, String token){
        String[] split = e.split("/");
        int length = split.length;
        String last = "voddrm.token."+token+"."+split[length-1];
        split[length-1] = last;
        String url = String.join("/",split);
        return url;
    }
/*
function (e, t, n) {
    var r = u.default.location.protocol;
    return "http:" != r && "https:" != r && (r = "https:"), "".concat(r).concat(d.SERVER_PATH, "/").concat(e, "/v2/").concat(t, "/").concat(n)
}
t = "1258712167"
n="3701925922065222593"
*/

    /**
     * 拼接播放信息的请求地址
     * @param appID
     * @param fileID
     * @return
     */
    public String assemblyPath(String appID,String fileID,Map<String,String> login){

        Set<String> set = login.keySet();
        String[] sign = new String[set.size()];
        int i = 0;
        for (String k : set) {
            sign[i] = k+"="+login.get(k);
            i++;
        }
        String param = String.join("&",sign);

        String r = "https:"+"//playvideo.qcloud.com"+"/"+"getplayinfo"+"/v2/"+appID+"/"+fileID+"?"+param;
        return r;
    }
    /*
function t() {
    var t = e.call(this) || this;
    return t.getBkn = function() {
    e = 'p_lskey':'0004000002723be2d151a41b5643e489333c6356d9ce39c91522a8118ee8b4f193a8ae3a5a04583ca7ce663e',
        return function(e) {
            if (!e)
                return "";
            for (var t = 5381, n = 0, r = e.length; n < r; ++n)
                t += (t << 5) + e.charAt(n).charCodeAt(0);
            return 2147483647 & t
        }((0,y.getBknAuth)())
    }
    ,
    t
}
     */
    public int getBkn(String p_lskey){
        int t = 5381;
        int r = p_lskey.length();
        for (int n = 0; n < r; n++) {
            t = t + (t << 5)+p_lskey.charAt(n);

        }
        return 2147483647 & t;

    }
}
